/*
 * Copyright 2010 Spolecne s.r.o. (www.spoledge.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.appengine.api.datastore;

import java.io.Serializable;

/**
 * This is a GWT emulation class.
 * It is ONLY used on the client side - JavaScript.
 *
 * This class is intended to use with custom DTO serializers
 * generated by AuDAO.
 *
 * Method hashCode() may differ from the original one.
 */
public class IMHandle implements Serializable, Comparable<IMHandle> {

    public static final long serialVersionUID = 6963426833434504530L;

    public enum Scheme { sip, unknown, xmpp }

    private String protocol;
    private String address;


    ////////////////////////////////////////////////////////////////////////////
    // Constructors
    ////////////////////////////////////////////////////////////////////////////

    public IMHandle( Scheme scheme, String address ) {
        if (scheme == null) throw new NullPointerException("Scheme cannot be null");
        if (address == null) throw new NullPointerException("Address cannot be null");

        this.protocol = scheme.name();
        this.address = address;
    }


    @SuppressWarnings("unused")
    private IMHandle() {
    }


    ////////////////////////////////////////////////////////////////////////////
    // Public
    ////////////////////////////////////////////////////////////////////////////

    public String getProtocol() {
        return protocol;
    }


    public String getAddress() {
        return address;
    }


    ////////////////////////////////////////////////////////////////////////////
    // Comparable
    ////////////////////////////////////////////////////////////////////////////

    public int compareTo( IMHandle o ) {
        int ret = protocol.compareTo( o.protocol );

        return ret != 0 ? ret : address.compareTo( o.address );
    }


    ////////////////////////////////////////////////////////////////////////////
    // Misc
    ////////////////////////////////////////////////////////////////////////////

    public boolean equals( Object o ) {
        if (this == o) return true;
        if (o == null || (!(o instanceof IMHandle))) return false;

        return compareTo( (IMHandle) o) == 0;
    }


    public int hashCode() {
        return protocol.hashCode() * 29 + address.hashCode();
    }


    public String toString() {
        return protocol + " " + address;
    }
}

